package com.yyplatform.admin.platform.schedule.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.schedule.model.dto.JobDTO;
import com.yyplatform.admin.platform.schedule.model.dto.JobFormDTO;
import com.yyplatform.admin.platform.schedule.model.dto.JobQueryDTO;
import com.yyplatform.admin.platform.schedule.service.JobService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sleepforests@163.com
 * <p>
 * Created on 2024/9/21 22:47
 */
@Slf4j
@Tag(name = "任务/任务管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/schedule/job/")
public class JobController {


    private JobService jobService;


    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:job:query")
    public RestResponse<Page<JobDTO>> page(JobQueryDTO pageQueryDTO) {
        log.debug("query dto is {}", pageQueryDTO);

        Page<JobDTO> page = jobService.query(pageQueryDTO);


        return RestResponse.ok(page);
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:job:get")
    public RestResponse<JobDTO> get(Long id) {
        log.debug("get by id is {}", id);

        JobDTO data = jobService.get(id);
        return RestResponse.ok(data);
    }


    @PostMapping("save")
    @Operation(summary = "保存")
    @RequiresPermissions("sys:job:save")
    public RestResponse<Void> save(@RequestBody JobFormDTO formDTO) {
        log.debug("save dto is {}", formDTO);
        jobService.save(formDTO);

        return RestResponse.ok(null);
    }


    @PostMapping("update")
    @Operation(summary = "修改")
    @RequiresPermissions("sys:job:update")
    public RestResponse<Void> update(@RequestBody JobFormDTO formDTO) {
        log.debug("update dto is {}", formDTO);

        jobService.update(formDTO);
        return RestResponse.ok(null);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:job:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);
        jobService.delete(id);
        return RestResponse.ok(null);
    }


    @PostMapping("doAction")
    @Operation(summary = "执行动作")
    @RequiresPermissions("sys:job:doAction")
    public RestResponse<Void> doAction(Long id, String action) {
        log.debug("do action by id is {}, action is {}", id, action);

        jobService.doAction(id, action);
        return RestResponse.ok(null);
    }

}
